package zerox

import (
	"errors"
	"strings"

	"github.com/go-playground/locales/zh"
	ut "github.com/go-playground/universal-translator"

	"github.com/go-playground/validator/v10"
	zh_tran "github.com/go-playground/validator/v10/translations/zh"
)

func ValidStruct(s interface{}) error {
	valid := validator.New()
	uni := ut.New(zh.New())
	trans, _ := uni.GetTranslator("zh")
	if err := zh_tran.RegisterDefaultTranslations(valid, trans); err != nil {
		return err
	}
	if err := valid.Struct(s); err != nil {
		errs := []string{}
		for _, err := range err.(validator.ValidationErrors) {

			errs = append(errs, err.Translate(trans))
		}
		return errors.New(strings.Join(errs, ","))

	}
	return nil
}

func ValidVal(s interface{}, tag string) error {
	valid := validator.New()
	uni := ut.New(zh.New())
	trans, _ := uni.GetTranslator("zh")
	if err := zh_tran.RegisterDefaultTranslations(valid, trans); err != nil {
		return err
	}
	if err := valid.Var(s, tag); err != nil {
		errs := []string{}
		for _, err := range err.(validator.ValidationErrors) {

			errs = append(errs, err.Translate(trans))
		}
		return errors.New(strings.Join(errs, ","))

	}
	return nil
}
